const App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fieldNames: {
      text: 'name',
      value: 'code',
      children: 'items',
    },
    form: {
      avatar: 'https://img.yzcdn.cn/vant/cat.jpeg',
      phone: "",
      name: "",
      sex: "",
      address: "",
    },
    show: {
      sex: false,
      bir: false,
      ade: false
    },
    actions: [{
        id: 0,
        name: '女',
      },
      {
        id: 1,
        name: '男',
      },
    ],
    date: new Date().getTime(),
    minDate: new Date('1970-1-1'),
    avatarUrl: "",
    nickname: ""
  },
  submit() {


    if (this.data.nickname == "") {
      App.showError("请输入昵称");
      return false;
    }

    if (this.data.avatarUrl == '') {
      App.showError("请上传头像");
      return false;
    }

    App._post_form('wxapi/updatehead', {
      nickname: this.data.nickname,
      avatarurl: this.data.avatarUrl
    }, function (result) {


      App.showSuccess("上传成功", function () {
        wx.navigateBack();
      });
    }, false, function () {

    });

  },
  upload(event) {
    console.log("sss", event)

    var avatarUrl = event.detail.file;

    var that = this;
    console.log("avatarUrl", avatarUrl, App.api_root)
    wx.uploadFile({
      url: App.api_root + 'upload/file',
      filePath: avatarUrl.url,
      name: 'imgFile',
      success: function (res) {

        console.log("data", JSON.parse(res.data).code)
        if (JSON.parse(res.data).code == 200) {

          that.setData({
            avatarUrl: JSON.parse(res.data).pic,

          })
        }
      }
    })
  },
  onChange(e) {
    console.log(e)
    let type = e.currentTarget.dataset.type;
    this.setData({
      ['show.' + type]: true
    })
  },
  onSelect(e) {
    console.log(e)
    let sex = e.detail.name;
    this.setData({
      ['form.sex']: sex,
      ['show.sex']: false
    })
  },
  addAddress(e) {
    let data = e.detail.values;
    this.setData({
      ['form.address']: data.map(item => item.name).join(' '),
      ['show.ade']: false
    })
  },
  addBir(e) {
    let time = e.detail
    this.setData({
      ['form.birthday']: formatTime(time),
      ['show.bir']: false
    })
  },
  cancel(e) {
    let type = e.currentTarget.dataset.type;
    this.setData({
      ['show.' + type]: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.getdata();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getdata() {

    let _this = this;

    App._post_form('wxapi/myinfo', {}, function (result) {
      if (result.status == "200") {
        _this.setData({
          avatarUrl: result.data.avatarurl,
          nickname: result.data.nickname
        });
      }

    });

  }
})